package com.gold.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gold.common.JsonUtils;
import com.gold.common.StringUtils;
import com.gold.entity.CardInfo;
import com.gold.entity.User;
import com.gold.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author XiaoHao
 * @date 2022-05-11 09:04
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 查询用户
     * @param parm
     * @param request
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    @ResponseBody
    public String getList(@RequestBody JSONObject parm, HttpServletRequest request) {
        int page = 1;
        int size = 10;
        User user = new User();
        if (parm != null) {
            String page1 = parm.getString("page");
            String size1 = parm.getString("size");
            if (StringUtils.isNotEmpty(page1)) {
                page = Integer.parseInt(page1);
            }
            if (StringUtils.isNotEmpty(size1)) {
                size = Integer.parseInt(size1);
            }
            user.setUsername(parm.getString("username"));
            user.setState(parm.getString("state"));
        }
        IPage<User> userIPage = userService.selectAll(page, size, user);
        if (userIPage == null) {
            return JsonUtils.responseJson(true, 200, "用户数据", "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "用户数据", (JSONObject) JSONObject.toJSON(userIPage)).toJSONString();
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@RequestBody User user) {
        try {
            userService.delete(user);
        } catch (Exception e) {
            return JsonUtils.responseJson(false, 400, "删除错误：" + e.getMessage(), "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "删除成功", "").toJSONString();
    }

    /**
     * 修改用户状态
     * @param user
     * @return
     */
    @PreAuthorize("hasAnyRole('admin')")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@RequestBody User user) {
        try {
            userService.update(user);
        } catch (Exception e) {
            return JsonUtils.responseJson(false, 400, "更新用户错误：" + e.getMessage(), "").toJSONString();
        }
        return JsonUtils.responseJson(true, 200, "更新用户成功", "").toJSONString();
    }
}
